import sys

from PySide6.QtCore import Signal
from PySide6.QtWidgets import QDialog, QApplication, QMessageBox

from backend.db.database import SessionLocal
from backend.services.members_service import MemberService
from front.ui.ui_delete_vip import Ui_deleter_vip


class DeleteVip(Ui_deleter_vip, QDialog):
    refresh = Signal()
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.delete_vip)

    def delete_vip(self):
        reply = QMessageBox.question(
            self,
            "确认删除VIP",
            "是否确认删除？此操作不可撤销。",
            QMessageBox.Yes | QMessageBox.No,  # 显示"是"和"否"按钮
            QMessageBox.No  # 默认选中"否"按钮
        )
        if reply == QMessageBox.Yes:
            data = self.lineEdit_2.text()
            try:
                num = int(data)
            except ValueError:
                QMessageBox.warning(self,"警告","请输入正确ID")
                return
            with SessionLocal() as session:
                member_service = MemberService(session)
                delete_results = member_service.delete_member(data)
                if delete_results:
                    QMessageBox.information(self,"成功","删除成功")
                    self.refresh.emit()
                    self.deleteLater()
                else :
                    QMessageBox.warning(self,"警告","ID不存在")



if __name__ == '__main__':
    app = QApplication([])
    win = DeleteVip()
    win.show()
    sys.exit(app.exec())
